#include "KeyboardController.h"
#include "Application.h"
#include <math.h>  

void cat::KeyboardController::treatEvent(sf::Event& event)
{
	sf::RenderWindow& window = cat::Application::current().getWindow();

	if(event.type == sf::Event::MouseButtonPressed)
	{
		if(event.mouseButton.button == sf::Mouse::Left)
		{
			_tank->fire();
		}
	}

	// translation
	{
		int x = 0, y = 0;
		if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
		{
			x--;
		}

		if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
		{
			x++;
		}

		if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
		{
			y--;
		}

		if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
		{
			y++;
		}

		if(x==0 && y==0)
		{
			_tank->setVelocity(0);
		}
		else
		{
			float angle = atan2f(y, x);
			float velocity = 1;

			_tank->move(angle, velocity);
		}
	}

	// cannon
	{
		sf::Vector2i mousePosition = sf::Mouse::getPosition(window);
		_tank->turretTarget(mousePosition.x, mousePosition.y);
	}
}

cat::KeyboardController::KeyboardController(Tank* tank)
{
	_tank = tank;
}

cat::KeyboardController::~KeyboardController(void)
{
}
